<template>
  <div class="header" :class="{ hide: headHide }">
    <div class="head-more" @click="head_title = true">
      <i class="iconfont icon-liebiao"></i>
    </div>
    <div class="head-logo">
      <img src="../assets/img/1.jpg" alt="" />
    </div>
    <nav class="head-menu">
      <div>
        <i class="iconfont">&#xe8c6;</i>
        <button>
          <router-link to="/" :class="{ hide: headHide }">首页</router-link>
        </button>
      </div>
      <div>
        <i class="iconfont">&#xe601;</i>
        <button>
          <router-link to="/mood" :class="{ hide: headHide }">心情</router-link>
        </button>
      </div>
      <div>
        <i class="iconfont">&#xe7f1;</i>
        <button>
          <router-link to="/pic" :class="{ hide: headHide }">图册</router-link>
        </button>
      </div>
      <div>
        <i class="iconfont">&#xe6ab;</i>
        <button>
          <router-link to="/project" :class="{ hide: headHide }"
            >项目</router-link
          >
        </button>
      </div>
      <div>
        <i class="iconfont">&#xe615;</i>
        <button>
          <router-link to="/about" :class="{ hide: headHide }"
            >关于</router-link
          >
        </button>
      </div>
      <div>
        <i class="iconfont">&#xe7bf;</i>
        <button>
          <router-link to="/webs" :class="{ hide: headHide }"
            >网站导航</router-link
          >
        </button>
      </div>
    </nav>
    <el-drawer
      :visible.sync="head_title"
      direction="ltr"
      :show-close="false"
      size="50%"
      custom-class="nav_bg"
      :append-to-body="true"
    >
      <div class="nav_title">
        <div class="title">
          <div>
            <img src="http://47.113.189.57:5000/head_icon.jpg" alt="" />
          </div>
          <h2>热泪常怀个人博客</h2>
        </div>
        <nav class="head-menu">
          <div>
            <i class="iconfont">&#xe8c6;</i>
            <button>
              <router-link to="/" :class="{ hide: headHide }">首页</router-link>
            </button>
          </div>
          <div>
            <i class="iconfont">&#xe601;</i>
            <button>
              <router-link to="/mood" :class="{ hide: headHide }"
                >心情</router-link
              >
            </button>
          </div>
          <div>
            <i class="iconfont">&#xe7f1;</i>
            <button>
              <router-link to="/pic" :class="{ hide: headHide }"
                >图册</router-link
              >
            </button>
          </div>
          <div>
            <i class="iconfont">&#xe6ab;</i>
            <button>
              <router-link to="/project" :class="{ hide: headHide }"
                >项目</router-link
              >
            </button>
          </div>
          <div>
            <i class="iconfont">&#xe615;</i>
            <button>
              <router-link to="/about" :class="{ hide: headHide }"
                >关于</router-link
              >
            </button>
          </div>
          <div>
            <i class="iconfont">&#xe7bf;</i>
            <button>
              <router-link to="/webs" :class="{ hide: headHide }"
                >网站导航</router-link
              >
            </button>
          </div>
        </nav>
      </div>
    </el-drawer>

    <nav class="head-social mouseCursorLink">
      <span @click="user_is_show = true" v-if="!showUser">登录</span>
      <div class="user_info" v-if="showUser">
        <img :src="user.avater" alt="" />
        <el-dropdown @command="handleCommand">
          <span class="el-dropdown-link" :class="{ hide: headHide }">
            {{ user.name }}<i class="el-icon-arrow-down el-icon--right"></i>
          </span>
          <el-dropdown-menu slot="dropdown">
            <el-dropdown-item command="person">个人中心</el-dropdown-item>
            <el-dropdown-item command="message">消息通知</el-dropdown-item>
            <el-dropdown-item command="changeAvater">修改头像</el-dropdown-item>
            <el-dropdown-item divided command="exit">注销退出</el-dropdown-item>
          </el-dropdown-menu>
        </el-dropdown>
      </div>
      <div class="theme mouseCursorLink">
        <div>
          <i class="iconfont" @click="addColor(false)">&#xe636;</i>
          <i class="iconfont" @click="addColor(false)">&#xe746;</i>
        </div>
      </div>
    </nav>
    <UserView v-show="user_is_show"></UserView>
    <div
      class="close_user"
      v-show="user_is_show"
      @click="user_is_show = false"
    ></div>
  </div>
</template>

<script>
import { addColor } from '../assets/theme/addColor'
import UserView from '@/components/User.vue'
import $ from 'jquery'
export default {
  name: 'Header',
  setup() {
    return {
      addColor,
    }
  },
  data() {
    return {
      headHide: true,
      head_title: false,
      user_is_show: false,
      user: {},
      showUser: false,
    }
  },
  methods: {
    scrollToTop() {
      var scrollTop =
        window.pageYOffset ||
        document.documentElement.scrollTop ||
        document.body.scrollTop
      scrollTop >= 200 ? (this.headHide = false) : (this.headHide = true)
    },
    // showTitle(){
    //   this.head_title = true
    //   $('div.header').parent().addClass('open')
    // }
    setUser() {
      return this.$store.getters.user
    },
    handleCommand(command) {
      switch (command) {
        case 'person':
          console.log('个人中心')
          break
        case 'message':
          console.log('消息中心')
          break

        case 'changeAvater':
          console.log('修改头像')
          break
        case 'exit':
          console.log('退出')
          this.logOut()
          break
      }
    },
    logOut() {
      localStorage.removeItem('eleToken')
      this.$store.dispatch('clearCurrentState')
      location.reload()
    },
  },
  mounted() {
    window.addEventListener('scroll', this.scrollToTop)
  },
  components: {
    UserView,
  },
  created() {
    this.user = this.setUser()
    if (this.user.id) {
      this.showUser = true
    }
  },
}
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>
.header {
  display: flex;
  background: var(--head-bg);
  color: var(--head-color);
  width: 100%;
  height: 80px;
  justify-content: space-between;
  align-items: center;
  box-shadow: 0 0 1em rgba(0, 0, 0, 0.1);
  transition: 0.3s;
  position: fixed;
  top: 0;
  left: 0;
  z-index: 1600;
}
.hide {
  background: transparent !important;
  box-shadow: 0 0 0 !important;
  color: white !important;
}
.header > div.head-more {
  display: none;
}
.header > div.head-more > i {
  margin-left: 45px;
  font-size: 30px;
}
.header > div.head-logo {
  height: 80px;
  width: 80px;
  display: flex;
  align-items: center;
  justify-content: center;
}
.header > div.head-logo > img {
  width: 60px;
  border-radius: 50%;
}
.header > nav.head-menu {
  display: flex;
}
.header > nav.head-menu > div {
  padding: 0 1em;
  line-height: 4em;
  /* cursor: pointer; */
}
.header > nav.head-menu > div > i {
  font-size: 20px;
  margin-right: 0.3em;
}
.header > nav.head-social {
  display: flex;
}
.header > nav.head-social > span {
  font-size: 18px;
  font-weight: 600;
}
.header > nav.head-social > .theme {
  height: 30px;
  width: 45px;
  overflow: hidden;
  position: relative;
}
.header > nav.head-social > .theme > div {
  position: absolute;
  top: var(--icon-top);
  left: 0;
  display: flex;
  flex-direction: column;
  transition: 0.3s;
  cursor: pointer;
}
.header > nav.head-social > .theme > div > i {
  margin-left: 0.5em;
  font-size: 30px;
}
.header > nav.head-social > .user_info {
  display: flex;
  align-items: center;
}
.header > nav.head-social > .user_info > img {
  display: block;
  width: 30px;
  border-radius: 50%;
  margin-right: 1em;
}
button {
  color: var(--main-color);
  font-size: 18px;
  font-weight: 800;
  position: relative;
  border: none;
  background: none;
  text-transform: uppercase;
  transition-timing-function: cubic-bezier(0.25, 0.8, 0.25, 1);
  transition-duration: 400ms;
  transition-property: color;
}
button:focus,
button:hover {
  color: var(--main-color);
}

button:focus:after,
button:hover:after {
  width: 100%;
  left: 0%;
}

button:after {
  content: '';
  pointer-events: none;
  bottom: -2px;
  left: 50%;
  position: absolute;
  width: 0%;
  height: 2px;
  background-color: var(--head-color);
  transition-timing-function: cubic-bezier(0.25, 0.8, 0.25, 1);
  transition-duration: 400ms;
  transition-property: width, left;
}
a {
  color: var(--head-color);
}
.nav_title {
  margin-top: 2em;
  display: flex;
  flex-direction: column;
  justify-content: center;
  color: var(--main-color);
}
.nav_title > .title {
  display: flex;
  flex-direction: column;
  align-items: center;
}
.nav_title > .title > div {
  width: 60px;
  height: 60px;
  overflow: hidden;
  border-radius: 50%;
  margin-bottom: 1em;
}
.nav_title > .title > div > img {
  height: 100%;
  margin-left: -10px;
}
.nav_title > .head-menu {
  margin-top: 1em;
  display: flex;
  flex-direction: column;
}
.nav_title > .head-menu > div {
  text-align: center;
  margin-bottom: 0.5em;
}
.nav_title > .head-menu > div > i {
  color: var(--head-color) !important;
}
.nav_title > .head-menu > div > button > a {
  color: var(--head-color) !important;
}

.close_user {
  position: fixed;
  left: 0;
  top: 0;
  width: 100%;
  height: 100%;
  background: rgba(0, 0, 0, 0.5);
}
.el-dropdown-link {
  color: var(--head-color);
}
</style>
